package com.along.game.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.along.framework.dao.base.IBaseDao;
import com.along.game.dao.UserInfoDAO;
import com.along.game.model.Gamescene;
import com.along.game.model.OnlineUser;
import com.along.game.model.UserInfo;

@Repository("userInfoDAO")
public class UserInfoDAOImpl implements UserInfoDAO {

	@Resource(name="baseDao")
	private IBaseDao<UserInfo,Long> dao;
	
	public void saveUserInfo(UserInfo userinfo) {
		dao.getHibernateTemplate().saveOrUpdate(userinfo);
		
	}
	
	
	public void saveOnlineUser(OnlineUser onlineUser) {
		dao.getHibernateTemplate().saveOrUpdate(onlineUser);
		
	}

	public List<UserInfo> getAllUserInfo() {
		return  dao.getHibernateTemplate().find("from UserInfo");
	}

	/**
	 * 
	 */
	public UserInfo getUserInfo(String usn) {
		return (UserInfo)dao.getHibernateTemplate().get(UserInfo.class, usn);
	}
	
	/**
	 * 通过用户名和密码得到用户信息
	 */
	public UserInfo getUserInfo(String usn,String pwd) {
		  Object[] o = {usn, pwd};
		    List l = dao.getHibernateTemplate().find("from UserInfo where usn=? and psw=?", o);
		    if (l == null || l.isEmpty()) {
		      return null;
		    }
		    else {
		      return (UserInfo) l.get(0);
		    }
	}
	
	/**
	 * 通过房间得到房间信息
	 */
	public OnlineUser getOnlineUserByDeskId(String deskId) {
		  Object[] o = {deskId};
		    List l = dao.getHibernateTemplate().find("from OnlineUser where deskId=?", o);
		    if (l == null || l.isEmpty()) {
		      return null;
		    }
		    else {
		      return (OnlineUser) l.get(0);
		    }
	}


	
}
